from flask import Flask, request, jsonify
from spider.tampermonkey.boss import BossZhiPinParser
import tld
import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from MonitorWidget import Ui_JobMonitor
import os

default_port = 5000

def getCurTime(f: int = 0):
    time = QDateTime.currentDateTime()  # 获取当前时间

    if f == 1:
        fmt = 'hh:mm:ss.zzz'
    elif f == 2:
        fmt = 'yyyy-MM-dd'
    elif f == 3:
        fmt = 'yyyy-MM-dd hh:mm:ss'
    elif f == 10:
        fmt = 'yyyy:MM:dd-hh:mm:ss.zzz'
    elif f == 20:
        fmt = 'yyyyMMddhhmmsszzz'
    else:
        fmt = 'hh:mm:ss'

    timedisplay = time.toString(fmt)  # 格式化一下时间

    return timedisplay


class JobNetwork(QThread):
    sigTransmission = pyqtSignal(dict)

    def __init__(self, name:str, port:int=5000):
        super().__init__(parent=None)
        self.name = name
        self.port = port

        self.handle = Flask(self.name)

        @self.handle.route('/parse', methods=['POST'])
        def _parse_html():
            print(f'network thread id:{int(QThread.currentThread().currentThreadId())}')
            self.sigTransmission.emit(request.get_json())
            return jsonify({'result': 'success'})

    def run(self):
        self.handle.run(port=self.port)

    def stop(self):
        self.handle.shutdown()
        self.quit()
        self.wait()

class JobParser(QObject):
    sigStarted = pyqtSignal()
    sigParser = pyqtSignal(str)

    def __init__(self, name:str, parent=None):
        super().__init__(parent=parent)
        self.name = name

    @pyqtSlot()
    def init(self):
        # 解析器初始化
        self.parserDict = {'zhipin.com': BossZhiPinParser}

        self.sigStarted.emit()

    @pyqtSlot(dict)
    def process(self, json:dict):
        print(f'parser thread id:{int(QThread.currentThread().currentThreadId())}')
        url = json['url']
        html = json['html']
        domain = tld.get_tld(url, as_object=True, fix_protocol=True).fld
        print(domain, url)

        if domain in self.parserDict:
            obj = self.parserDict[domain](html)
            info = obj.put_info()

            self.sigParser.emit(info)
        else:
            self.sigParser.emit(f'Can not Parser url:{url}\n{html}')

class JobSpider(QtWidgets.QWidget, Ui_JobMonitor):
    def __init__(self):
        super().__init__()

        # UI 初始化
        self.setupUi(self)
        # self.showMaximized()

        self.name = 'JobSpider'
        self.setWindowTitle(self.name)

        # 信号槽初始化
        self.signalSoltInit()

        print(f'main thread id:{int(QThread.currentThread().currentThreadId())}')

    def signalSoltInit(self):
        # 日志初始化
        self.workPath = os.path.join(os.path.expanduser("~"), 'Desktop', self.name)
        if not os.path.exists(self.workPath):
            os.mkdir(self.workPath)

        self.logFileName = os.path.join(self.workPath, f'JobSpider_{getCurTime(20)}.txt')
        self.logFile = QFile(self.logFileName)
        self.logFile.open(QIODevice.ReadWrite | QIODevice.Text)
        self.logText = QTextStream(self.logFile)
        self.logText << f'[{getCurTime(1)}]Init Finish\n'
        self.logText.flush()

        # 网络对象初始化
        self.network = JobNetwork(self.name)

        # 解析器初始化
        loop = QEventLoop()
        self.thread = QThread()
        self.parser = JobParser(self.name)
        self.thread.started.connect(self.parser.init)
        self.parser.sigStarted.connect(loop.quit)
        self.parser.moveToThread(self.thread)
        self.thread.start()
        loop.exec_()

        self.network.sigTransmission.connect(self.parser.process)
        self.parser.sigParser.connect(self.updateLogInfo)
        self.network.start()

        # 定时器设置
        self.timeTimer = QTimer()
        self.timeTimer.timeout.connect(self.showTime)
        self.timeTimer.start(1000)

    def updateLogInfo(self, info:str):
        # self.logView.append(info)

        self.logText << info << '\n'
        self.logText.flush()

    def showTime(self):
        self.dateLabel.setText(getCurTime(2))
        self.timeLabel.setText(getCurTime())

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = JobSpider()
    window.show()
    sys.exit(app.exec_())
